package guiPackage;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class DataScherm extends JPanel {
	private String[][][] data;
	private HashMap<String, JTextField> aantalRecordsFields;
	private Box tabellenBox;
	private JScrollPane scrollPane;
	private JButton maakQueriesButton, terugButton;
	private static OpvulMethoden[] opvulMethoden;

	public DataScherm(String[][][] data, int aantalKolommen) {
		this.data = data;
		opvulMethoden = new OpvulMethoden[aantalKolommen];
		int methodeNummer = 0;
		tabellenBox = Box.createVerticalBox();
		scrollPane = new JScrollPane(tabellenBox);
		aantalRecordsFields = new HashMap<String, JTextField>();
		maakQueriesButton = new JButton("Maak Queries");
		terugButton = new JButton("Terug");
		maakQueriesButton.setFont(new Font("sansserif", Font.BOLD, 30));
		terugButton.setFont(new Font("sansserif", Font.BOLD, 30));
		for (int i = 0; i < data.length; i++) {
			JPanel formPanel = new JPanel();
			formPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
			JPanel tabelNaamPanel = new JPanel();
			tabelNaamPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
			if (data[i][0][0] != null) {
				// tabelnaam
				JLabel tabelNaam = new JLabel(data[i][0][0]);
				tabelNaam.setFont(new Font("sansserif", Font.BOLD, 32));
				tabelNaamPanel.add(tabelNaam);
				formPanel.add(new JLabel("Aantal records: "));
				JTextField aantalRecordsField = new JTextField(3);
				aantalRecordsField.setText("0");
				formPanel.add(aantalRecordsField);
				aantalRecordsFields.put(data[i][0][0], aantalRecordsField);
				tabellenBox.add(tabelNaamPanel);
				tabellenBox.add(formPanel);
				for (int j = 1; j < data[i].length; j++) {
					if (data[i][j][0] != null) {
						 JPanel kolomPanel = new JPanel();
						kolomPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
						// kolomnaam
						kolomPanel.add(new JLabel(data[i][j][0] + ": "));
						// kolomtype
						kolomPanel.add(new JLabel(data[i][j][1]));

						JPanel methodenPanel = new JPanel();
						opvulMethoden[methodeNummer] = new OpvulMethoden(methodenPanel);
						methodenPanel = opvulMethoden[methodeNummer].getMethodenPanel(data[i][j][1]);
						opvulMethoden[methodeNummer].setTabelNaam(data[i][0][0]);
						opvulMethoden[methodeNummer].setKolomNaam(data[i][j][0]);
						methodeNummer++;
						kolomPanel.add(methodenPanel);
						tabellenBox.add(kolomPanel);
					}
				}
			}
			tabellenBox.add(Box.createVerticalStrut(30));
		}
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1,2));
		buttonPanel.add(terugButton);
		buttonPanel.add(maakQueriesButton);
		scrollPane.setPreferredSize(new Dimension(1000, 900));
		setLayout(new BorderLayout());
		add(scrollPane, BorderLayout.CENTER);
		add(buttonPanel, BorderLayout.SOUTH);
	}

	public HashMap<String, JTextField> getAantalRecordsFields() {
		return aantalRecordsFields;
	}

	public void setListener(ActionListener listener) {
		maakQueriesButton.addActionListener(listener);
		terugButton.addActionListener(listener);
	}

	public JButton getMaakQueriesButton() {
		return maakQueriesButton;
	}

	public JButton getTerugButton() {
		return terugButton;
	}

	public static OpvulMethoden[] getOpvulMethoden() {
		return opvulMethoden;
	}
}
